package component.controls
{
	import component.controls.interfaces.IListCell;
	import component.controls.interfaces.IListCellData;
	import component.controls.ui.Component;
	import component.controls.ui.ListCell;
	import component.global.ComponentSetting;
	import component.global.manager.EventMgr;
	import component.global.utils.DisplayUtils;
	import component.global.utils.GraphicsUtils;
	import component.global.utils.ObjectUtil;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	
	public class List extends Component
	{
		public function List($width:int = ComponentSetting.LIST_DEFAULT_WIDTH, $height:int = ComponentSetting.LIST_DEFAULT_HEIGHT)
		{
			_width = $width;
			_height = $height;
			super();
		}
		
		protected var _container:Sprite;
		protected var _data:Array;
		protected var _cells:Vector.<IListCell>;
		protected var _scrollBar:ScrollBar;
		protected var _mask:Shape;
		protected var _cellClass:String;
		protected var _spacing:int;
		
		override protected function init():void{
			super.init();
			_cellClass = "";
			_spacing = 1;
			_cells = new Vector.<IListCell>();
			_data = [];
			_mask = GraphicsUtils.createRectangle(_width, _height);
			_container = new Sprite();
			_scrollBar = new ScrollBar(_height);
			addChild(_mask);
			mask = _mask;
			
			DisplayUtils.addDisplayObject(this, _container);
		}
		
		public function scroll(value:int = 0):void{
			_scrollBar.scrollTo(_scrollBar.scrollPos + value);
		}
		
		public function set cellClass(value:String):void{
			_cellClass = value;
		}
		
		public function set spacing(value:int):void{
			_spacing = value;
		}
		
		public function get spacing():int{
			return _spacing;
		}
		
		public function set dataProvider(arr:Array):void{
			if(arr == _data)return;
			var extraLen:int
			if(_data == null){
				extraLen = arr.length;
			} else {
				extraLen = arr.length - _data.length;
			}
				
			if(extraLen <= 0){
				for (var j:int = 0; j < -extraLen; j++) 
				{
					var cell:* = _cells.pop();
					ObjectUtil.dispose(cell);
					cell = null;
				}
			} else {
				for (var k:int = 0; k < extraLen; k++) 
				{
					if(_cellClass != ""){
						var cls:Class = getDefinitionByName(_cellClass) as Class;
						cell = new cls();
					} else {
						cell = new ListCell(_data[i].label,_width - 2);
					}
					_cells.push(cell);
				}
			}
			ObjectUtil.disposeElements(_data);_data = null;
			_data = arr;
			for(var i:int = 0;i<_data.length ; i++){
				_cells[i].data = _data[i];
				_cells[i].asDisplayObject.y = i * (_cells[i].asDisplayObject.height + _spacing);
				EventMgr.Instance.addEvent(_cells[i] as EventDispatcher,[MouseEvent.CLICK],[__onItemClick]);
				DisplayUtils.addDisplayObject(_container, _cells[i]);
			}
//			if(_data.length > 0 && _container.height > _scrollBar.height){
				_scrollBar.targetDisplay = _container;
//			}
		}
		
		public function addCell(data:IListCellData, idx:int = 0):void{
			
		}
		
		public function removeCell(data:IListCellData, idx:int = 0):void{
			
		}
		
		private function clearAllCells():void{
			ObjectUtil.disposeElements(_data);_data = null;
			ObjectUtil.disposeElements(_cells);_cells=null;
			_cells = new Vector.<IListCell>();
			_data = [];
		}
		
		private function __onItemClick(e:MouseEvent):void{
			var cell:IListCell = e.currentTarget as IListCell;
			for(var i:int = 0;i<_cells.length ; i++){
				_cells[i].selected = false;
			}
			cell.selected = true;
		}
		
		override public function dispose():void{
			super.dispose();
			clearAllCells();
			_data = null;
			_cells = null;
			ObjectUtil.dispose(_mask); _mask = null;
			ObjectUtil.dispose(_container); _container = null;
			ObjectUtil.dispose(_scrollBar); _container = null;
		}
	}
}